<?php

namespace App\Http\Controllers\Game\Other;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\Game\Other\Dictionary\StoreRequest;
use App\Http\Resources\Common\DatasResource;
use App\Http\Resources\Common\DataResource;
use App\Models\Game\Common\Dictionary;
use App\Services\Game\Other\DictionaryService;

class DictionaryController extends Controller
{

    /**
     * 获取数据列表
     */
    public function getList(Request $request)
    {
        list ($collection, $total) = DictionaryService::make()->getList($request);

        return DatasResource::make($collection)->setPaginateTotal($total);
    }

    /**
     * 创建数据
     */
    public function store(StoreRequest $request)
    {
        $dictionary = DictionaryService::make()->store($request);;

        return new DataResource($dictionary);
    }

    /**
     * 删除数据
     */
    public function destroy(Request $request)
    {
        $this->validate($request, [
            'id' => 'required|integer|min:1'
        ]);
        $id = $request->input('id');

        $dictionary = Dictionary::findOrFail($id);
        $dictionary->delete();

        return new DataResource($dictionary);
    }
}
